package net.guerlab.sdk.anubis;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.RandomUtils;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import net.guerlab.sdk.anubis.entity.AnubisOrderAddInfo;
import net.guerlab.sdk.anubis.entity.CancelInfo;
import net.guerlab.sdk.anubis.entity.ComplaintInfo;
import net.guerlab.sdk.anubis.entity.ItemInfo;
import net.guerlab.sdk.anubis.entity.ReceiverInfo;
import net.guerlab.sdk.anubis.entity.TransportInfo;
import net.guerlab.sdk.anubis.request.OrderCancelRequest;
import net.guerlab.sdk.anubis.request.OrderComplaintRequest;
import net.guerlab.sdk.anubis.request.OrderCreateRequest;
import net.guerlab.sdk.anubis.request.OrderQueryRequest;
import net.guerlab.sdk.anubis.response.OrderCancelResponse;
import net.guerlab.sdk.anubis.response.OrderComplaintResponse;
import net.guerlab.sdk.anubis.response.OrderCreateResponse;
import net.guerlab.sdk.anubis.response.OrderQueryResponse;

/**
 * 订单 demo
 *
 * @author guer
 *
 */
public class OrderDemo {

    private static final Logger LOGGER = LoggerFactory.getLogger(OrderDemo.class);

    /**
     * 创建订单
     */
    @Test
    public void create() {
        TransportInfo transportInfo = new TransportInfo();
        transportInfo.setTransportName("web-base-test");
        transportInfo.setTransportAddress("300弄亚都国际名园5号楼2003室");
        transportInfo.setTransportLongitude(BigDecimal.valueOf(121.3718891000));
        transportInfo.setTransportLatitude(BigDecimal.valueOf(31.2306375000));
        transportInfo.setPositionSource(1);
        transportInfo.setTransportTel("13558911947");
        transportInfo.setTransportRemark("备注");

        ReceiverInfo receiverInfo = new ReceiverInfo();
        receiverInfo.setReceiverName("李明");
        receiverInfo.setReceiverPrimaryPhone("13900000000");
        receiverInfo.setReceiverSecondPhone("13911111111");
        receiverInfo.setReceiverAddress("上海市近铁广场");
        receiverInfo.setReceiverLongitude(BigDecimal.valueOf(121.3718891000));
        receiverInfo.setReceiverLatitude(BigDecimal.valueOf(31.2306475000));
        receiverInfo.setPositionSource(1);

        List<ItemInfo> itemInfos = new ArrayList<>(1);

        ItemInfo itemInfo = new ItemInfo();
        itemInfo.setItemId("1");
        itemInfo.setItemName("苹果");
        itemInfo.setItemQuantity(5);
        itemInfo.setItemPrice(BigDecimal.valueOf(10.00));
        itemInfo.setItemActualPrice(BigDecimal.valueOf(9.80));
        itemInfo.setItemSize(1);
        itemInfo.setItemRemark("商品备注");
        itemInfo.setIsNeedPackage(1);

        itemInfos.add(itemInfo);

        AnubisOrderAddInfo anubisOrderAddInfo = new AnubisOrderAddInfo();
        anubisOrderAddInfo.setPartnerOrderCode("test" + RandomUtils.nextInt(1000, 10000));
        anubisOrderAddInfo.setNotifyUrl("http://101.207.125.108:7575/commons/test");
        anubisOrderAddInfo.setTransportInfo(transportInfo);
        anubisOrderAddInfo.setOrderAddTime(System.currentTimeMillis());
        anubisOrderAddInfo.setOrderTotalAmount(BigDecimal.valueOf(50.00));
        anubisOrderAddInfo.setOrderActualAmount(BigDecimal.valueOf(49.00));
        anubisOrderAddInfo.setOrderWeight(BigDecimal.valueOf(3.5));
        anubisOrderAddInfo.setOrderRemark("订单备注");
        anubisOrderAddInfo.setIsInvoiced(1);
        anubisOrderAddInfo.setInvoice("xxx公司");
        anubisOrderAddInfo.setOrderPaymentStatus(1);
        anubisOrderAddInfo.setOrderPaymentMethod(1);
        anubisOrderAddInfo.setIsAgentPayment(0);
        anubisOrderAddInfo.setRequirePaymentPay(BigDecimal.valueOf(50.00));
        anubisOrderAddInfo.setGoodsCount(4);
        anubisOrderAddInfo.setRequireReceiveTime(System.currentTimeMillis() + 1000 * 60);
        anubisOrderAddInfo.setSerialNumber("1234");
        anubisOrderAddInfo.setReceiverInfo(receiverInfo);
        anubisOrderAddInfo.setItemInfos(itemInfos);

        OrderCreateRequest request = new OrderCreateRequest();
        request.setAnubisOrder(anubisOrderAddInfo);

        OrderCreateResponse response = DemoConfig.CLIENT.execute(request);

        LOGGER.debug("order add result : ", response.getData());
    }

    /**
     * 查询
     */
    @Test
    public void query() {
        OrderQueryRequest request = new OrderQueryRequest();
        request.setPartnerOrderCode("OGtCvxN");

        OrderQueryResponse response = DemoConfig.CLIENT.execute(request);

        LOGGER.debug("order query result : ", response.getData());
    }

    /**
     * 取消订单
     */
    @Test
    public void cancel() {
        CancelInfo cancelInfo = new CancelInfo();
        cancelInfo.setOrderCancelCode(3);
        cancelInfo.setOrderCancelDescription("买家取消订单");
        cancelInfo.setOrderCancelReasonCode(2);
        cancelInfo.setOrderCancelTime(System.currentTimeMillis());
        cancelInfo.setPartnerOrderCode("OGtCvxN");

        OrderCancelRequest request = new OrderCancelRequest();
        request.setCancelInfo(cancelInfo);

        OrderCancelResponse response = DemoConfig.CLIENT.execute(request);

        LOGGER.debug("order cancel result : ", response.getData());
    }

    /**
     * 投诉
     */
    @Test
    public void complaint() {
        ComplaintInfo complaintInfo = new ComplaintInfo();
        complaintInfo.setOrderComplaintCode(230);
        complaintInfo.setOrderComplaintDesc("投诉测试");
        complaintInfo.setOrderComplaintTime(System.currentTimeMillis());
        complaintInfo.setPartnerOrderCode("PztuJh5");

        OrderComplaintRequest request = new OrderComplaintRequest();
        request.setComplaint(complaintInfo);

        OrderComplaintResponse response = DemoConfig.CLIENT.execute(request);

        LOGGER.debug("order complaint result : ", response.getData());
    }
}
